﻿using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Printing;
using System.Text;
using GameData;
using Cmd;
using UnityEngine.UI;
using DG.Tweening;
using Color = System.Drawing.Color;
using Debug = UnityEngine.Debug;


public class ReceiveUI : MonoBehaviour {

    public GameObject recBtn;
    public Text tipText;
    public bool isRec = false;

    public GameObject tipPanel;
    public Text numText;
    public GameObject enBtn;

    public GameObject backBtn;

    public PrintDocument printDocument;
    private System.Drawing.Image im;
    private bool isPrint = true;

    private bool isloadcard = false;
    private int iRetUSB = 0;
    private int iRetCOM = 0;
    //

    public bool isRecNum = true;


    public GameObject btnToObj;
    public GameObject numToObj;
    private NumPanel NumPanel;
    //
    public CustomerInfo CustomerInfo;
    public GameObject cusBtn;


    // Use this for initialization
    void Start () {
        Screen.sleepTimeout = SleepTimeout.NeverSleep;
        Messenger<NumAllInfo>.AddListener("SC统计数据", OnNumberInfo);
        Messenger<bool>.AddListener("SC设置取号方式", OnRecNumResponse);

        EventTriggerListener.Get(recBtn).onClick = OnClickRec;
        EventTriggerListener.Get(enBtn).onClick = OnClicken;
        EventTriggerListener.Get(backBtn).onClick = OnClickBack;
        EventTriggerListener.Get(cusBtn).onClick = OncusBtn;


        tipPanel.transform.localScale = new Vector3(0, 0, 0);

        NumPanel = numToObj.GetComponent<NumPanel>();

        //发送统计数据
        Cmd.ReceiveCountSend rcs = new ReceiveCountSend();
        rcs.Send(GlobalVO.GetInstance().roleInfo.UserInfo, OnNumberInfo);

        GetRecNumType getRecNumType = new GetRecNumType();
        getRecNumType.Send(GlobalVO.GetInstance().roleInfo.UserInfo, OnGetRecNum);



        Debug.Log(Application.platform);
        if (isPrint)
        {
            if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor)
            {
                printDocument = new PrintDocument();
                printDocument.DefaultPageSettings.PaperSize = new PaperSize("6cun", 100, 350);
                printDocument.PrintPage += new PrintPageEventHandler(this.printDocument_PrintPage);
            }
        }
        //



    }


    void OnRecNumResponse(bool res)
    {
        isRecNum = res;
        //
        numToObj.SetActive(res);
        btnToObj.SetActive(!res);
        if (res)
        {
            NumPanel.OnStartLoading();
        }

    }

    void OnGetRecNum(RecNumType recNumType)
    {
        if (GlobalVO.GetInstance().NumAllInfo!=null)
        {
            GlobalVO.GetInstance().NumAllInfo.isRecIdCard = recNumType.reNumType;

        }
        OnRecNumResponse(recNumType.reNumType);
        //


    }

    public void OnRemoveListener()
    {
        Messenger<NumAllInfo>.RemoveListener("SC统计数据", OnNumberInfo);
        Messenger<bool>.RemoveListener("SC设置取号方式", OnRecNumResponse);
        CustomerInfo.OnRemoveListener();
        NumPanel.OnStopLoading();
        int isSuccess = CVRSDK.CVR_CloseComm();
    }

    void OnClickBack(GameObject obj)
    {

        OnRemoveListener();


        Application.LoadLevel("Main");

    }

    void OnClickRec(GameObject obj)
    {
        if(!isRec)
        {
            ReceiveNumSend ns = new ReceiveNumSend();
            ns.Send(GlobalVO.GetInstance().roleInfo.UserInfo, OnNumberInfo);
            isRec = true;
        }

    }


    void OnNumberInfo(NumberInfo numberinfo)
    {
        Tweener tw = tipPanel.transform.DOScale(new Vector3(1, 1, 1), 0.3f).SetEase(Ease.OutBack);
        Debug.Log("领取号码:" + PrintNumberInfo.groupTag+ numberinfo.value);
        numText.text = numberinfo.value.ToString();
        if (!isPrint)
        {
            return;
        }
        if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor)
        {
            if (printDocument!= null)
            {
                try
                {
                    PrintNumberInfo = numberinfo;
                    Debug.Log("===========开始打印");
                    printDocument.Print();
                }
                catch (Exception e)
                {
                    Debug.Log(e);
                    throw;
                }


            }
        }
        //PrintDocument printDocument;
        
    }

    public Action YPrinAction;
    public NumberInfo PrintNumberInfo;
    public void StartPrint(Action ac, List<NumberInfo> numbreList)
    {
        YPrinAction = ac;
        Debug.Log("开始打印========================");
        if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor)
        {
            if (printDocument != null)
            {
                StartCoroutine(PrintLoad(numbreList));


            }
        }
        
    }


    public IEnumerator PrintLoad(List<NumberInfo> numbreList)
    {
        for (int i = 0; i < numbreList.Count; i++)
        {
            PrintNumberInfo = numbreList[i];
            Debug.Log("打印中ing......");
            Debug.Log("打印========================"+ PrintNumberInfo.value);

            //Debug.Log("打印啦：===="+ PrintNumberInfo.value);
            printDocument.Print();
            Debug.Log("打印完=====");

            yield return 1;
        }
        YPrinAction();

    }

    private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
    {
        Debug.Log("打印了");
        e.Graphics.Clear(Color.FloralWhite);
        /*
            im = Image.FromFile(Application.dataPath+"/back.jpg");
            if (im != null)
            {
                e.Graphics.DrawImage(im, new Point(0, 0));
                im.Dispose();
            }
        */
        DateTime dateTime = DateTime.Now;
        string str = dateTime.ToString("yyyy年MM月dd日 hh:mm:ss");

        if (PrintNumberInfo.PrintCount > 1)
        {
            str += "  二次取票";
        }

        e.Graphics.DrawString("          售楼排号", new System.Drawing.Font(new FontFamily("宋体"), 13, System.Drawing.FontStyle.Bold), System.Drawing.Brushes.Black, 0, 0);
        e.Graphics.DrawString(str, new System.Drawing.Font(new FontFamily("宋体"), 10, System.Drawing.FontStyle.Bold), System.Drawing.Brushes.Black, 0, 28);
        if (PrintNumberInfo.isNew)
        {
            e.Graphics.DrawString("新客户", new System.Drawing.Font(new FontFamily("宋体"), 10, System.Drawing.FontStyle.Bold), System.Drawing.Brushes.Black, 0, 58);

        }
        else
        {
            string idcatd = PrintNumberInfo.idCrad;
            if (PrintNumberInfo.idCrad.Length>6)
            {
                idcatd = idcatd.Substring(PrintNumberInfo.idCrad.Length - 6, 6);
            }
            e.Graphics.DrawString(PrintNumberInfo.@group+" 客户：" + PrintNumberInfo.name+"  "+ idcatd, new System.Drawing.Font(new FontFamily("宋体"), 10, System.Drawing.FontStyle.Bold), System.Drawing.Brushes.Black, 0, 58);

        }
        //



        e.Graphics.DrawString("-------------------------------------", new System.Drawing.Font(new FontFamily("宋体"), 10, System.Drawing.FontStyle.Bold), System.Drawing.Brushes.Black, 0, 75);

        e.Graphics.DrawString(UtilTool.getNumbertoStr(PrintNumberInfo.value) +"号", new System.Drawing.Font(new FontFamily("宋体"), 20, System.Drawing.FontStyle.Bold), System.Drawing.Brushes.Black, 110, 115);

        e.Graphics.DrawString("-------------------------------------", new System.Drawing.Font(new FontFamily("宋体"), 10, System.Drawing.FontStyle.Bold), System.Drawing.Brushes.Black, 0, 160);
        e.Graphics.DrawString("本票据当天有效", new System.Drawing.Font(new FontFamily("宋体"), 9, System.Drawing.FontStyle.Bold), System.Drawing.Brushes.Black, 0, 188);
        e.Graphics.DrawString("开发商：广东筠城置业有限公司", new System.Drawing.Font(new FontFamily("宋体"), 9, System.Drawing.FontStyle.Bold), System.Drawing.Brushes.Black, 0, 210);
        e.Graphics.DrawString("地址：新兴县新城镇惠中路1号", new System.Drawing.Font(new FontFamily("宋体"), 9, System.Drawing.FontStyle.Bold), System.Drawing.Brushes.Black, 0, 235);
        e.Graphics.DrawString("售楼热线：2929777", new System.Drawing.Font(new FontFamily("宋体"), 9, System.Drawing.FontStyle.Bold), System.Drawing.Brushes.Black, 0, 250);


        /*
         
        QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
        str += " 号码：";
        str += numText.text;
        
        Image im = new Bitmap(qrCodeEncoder.Encode(str, Encoding.UTF8),new Size(50,50));
        e.Graphics.DrawImage(im,new Point(50,40));
        */
    }



    void OnPrint(string path)
    {

        Process pro = new Process();
        pro.StartInfo.CreateNoWindow = true;
        pro.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        pro.StartInfo.UseShellExecute = true;
        pro.StartInfo.FileName = "path";
        pro.StartInfo.Verb = "print";
        pro.Start();

    }


    void OnNumberInfo(NumAllInfo numallInfo)
    {
        Debug.Log("推送了消息过来");
        tipText.text = "已经领取数量：" + numallInfo.CurCount;
        //


    }

    void OnClicken(GameObject obj)
    {
        tipPanel.transform.DOScale(new Vector3(0, 0, 0), 0.3f).SetEase(Ease.OutBack);
        isRec = false;
    }

    

    void OncusBtn(GameObject obj)
    {
        CustomerInfo.gameObject.SetActive(true);
        CustomerInfo.transform.localScale = new Vector3(0, 0, 0);
        Tweener tw = CustomerInfo.transform.DOScale(new Vector3(1, 1, 1), 0.3f).SetEase(Ease.OutBack);

    }
    // Update is called once per frame
    void Update () {
	
	}
}
